libobs_sources\windows\sources/
game_capture.rs

1use libobs_window_helper::{get_all_windows, WindowInfo, WindowSearchMode};
2use libobs_wrapper::{data::StringEnum, sources::ObsSourceRef};
3
4use crate::macro_helper::define_object_manager;
5
6use super::{ObsHookRate, ObsWindowPriority};
7
8#[derive(Clone, Copy, Debug, PartialEq, Eq)]
9/// Describes the capture mode of the game capture source.
10pub enum ObsGameCaptureMode {
11    /// Captures any fullscreen application
12    Any,
13    /// Captures a specific window, specified under the `window` property
14    CaptureSpecificWindow,
15    /// CApture the foreground window when a hotkey is pressed
16    CaptureForegroundWindow,
17}
18
19#[derive(Clone, Copy, Debug, PartialEq, Eq)]
20pub enum ObsGameCaptureRgbaSpace {
21    /// sRGB color space
22    SRgb,
23    /// Rec. 2100 (PQ)
24    RGBA2100pq,
25}
26
27impl StringEnum for ObsGameCaptureRgbaSpace {
28    fn to_str(&self) -> &str {
29        match self {
30            ObsGameCaptureRgbaSpace::SRgb => "sRGB",
31            ObsGameCaptureRgbaSpace::RGBA2100pq => "Rec. 2100 (PQ)",
32        }
33    }
34}
35
36impl StringEnum for ObsGameCaptureMode {
37    fn to_str(&self) -> &str {
38        match self {
39            ObsGameCaptureMode::Any => "any_fullscreen",
40            ObsGameCaptureMode::CaptureSpecificWindow => "window",
41            ObsGameCaptureMode::CaptureForegroundWindow => "hotkey",
42        }
43    }
44}
45
46define_object_manager!(
47    #[derive(Debug)]
48    struct GameCaptureSource("game_capture") for ObsSourceRef {
49        /// Sets the capture mode for the game capture source. Look at doc for `ObsGameCaptureMode`
50        #[obs_property(type_t = "enum_string")]
51        capture_mode: ObsGameCaptureMode,
52
53        /// Sets the window to capture.
54        ///
55        /// # Arguments
56        ///
57        /// * `window` - The window to capture, represented as `ObsString`. Must be in the format of an obs window id
58        ///
59        /// # Returns
60        ///
61        /// The updated `WindowCaptureSourceBuilder` instance.
62        #[obs_property(type_t = "string", settings_key = "window")]
63        window_raw: String,
64
65        #[obs_property(type_t = "enum")]
66        /// Window Match Priority
67        priority: ObsWindowPriority,
68
69        #[obs_property(type_t = "bool")]
70        /// SLI/Crossfire Capture Mode (Slow)
71        sli_compatability: bool,
72
73        #[obs_property(type_t = "bool")]
74        /// Whether the cursor should be captured
75        capture_cursor: bool,
76
77        #[obs_property(type_t = "bool")]
78        /// If transparency of windows should be allowed
79        allow_transparency: bool,
80
81        #[obs_property(type_t = "bool")]
82        /// Premultiplied Alpha
83        premultiplied_alpha: bool,
84
85        /// Limit capture framerate
86        #[obs_property(type_t = "bool")]
87        limit_framerate: bool,
88
89        /// Capture third party overlays (such as steam overlays)
90        #[obs_property(type_t = "bool")]
91        capture_overlays: bool,
92
93        /// Use anti-cheat compatibility hook
94        #[obs_property(type_t = "bool")]
95        anti_cheat_hook: bool,
96
97        /// Hook rate (Ranging from slow to fastest)
98        #[obs_property(type_t = "enum")]
99        hook_rate: ObsHookRate,
100
101        /// The color space to capture in
102        #[obs_property(type_t = "enum_string")]
103        rgb10a2_space: ObsGameCaptureRgbaSpace,
104
105        /// Whether to capture audio from window source (BETA) <br>
106        /// When enabled, creates an "Application Audio Capture" source that automatically updates to the currently captured window/application. <br>
107        /// Note that if Desktop Audio is configured, this could result in doubled audio.
108        #[obs_property(type_t = "bool")]
109        capture_audio: bool,
110    }
111);
112
113#[cfg(feature = "window-list")]
114impl GameCaptureSourceBuilder {
115    /// Gets a list of windows that can be captured by this source.
116    pub fn get_windows(mode: WindowSearchMode) -> anyhow::Result<Vec<WindowInfo>> {
117        get_all_windows(mode).map(|e| e.into_iter().filter(|x| x.is_game).collect::<Vec<_>>())
118    }
119
120    /// Sets the window to capture.
121    ///
122    /// # Arguments
123    ///
124    /// * `window` - The window to capture. A list of available windows can be retrieved using `GameCaptureSourceBuilder::get_windows`
125    ///
126    /// # Returns
127    ///
128    /// The updated `GameCaptureSourceBuilder` instance.
129    pub fn set_window(self, window: &WindowInfo) -> Self {
130        self.set_window_raw(window.obs_id.as_str())
131    }
132}